<template>
  <!-- 首页推荐 -->
  <div class="app-container">
    <el-card
      class="filter-container"
      shadow="never"
    >
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          size="small"
          @click="searchRecommendList()"
        >
          查询搜索
        </el-button>
        <el-button
          @click="handleResetSearch()"
          style="float:right;margin-right: 15px"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="关键字">
            <el-input
              v-model="listQuery.name"
              class="input-width"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <!-- 数据列表 -->
    <el-card
      class="operate-container"
      shadow="never"
    >
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>

    <!-- 数据展示 -->
    <div class="table-container">
      <el-table
        ref="orderTable"
        :data="list"
        style="width: 100%;"
        @selection-change="handleSelectionChange"
        border
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column
          label="ID"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column
          label="标题"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column
          label="状态"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.status==="1" ? "生效中" :"未生效" }}</template>
        </el-table-column>
        <el-table-column
          label="生效日期"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.releaseTime | formatCreateTime }}</template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.$index, scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { recommendList, recommendDelById } from "@/api/banner";
import { formatDate } from "@/utils/date";

const defaultListQuery = {
  sourceId: null,
  sourceType: null,
  channel: null,
  name: null,
  pageNum: 1,
  pageSize: 10,
};

export default {
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      // 选择的复选框
      multipleSelection: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 重置
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 筛选搜索
    searchRecommendList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleDelete(index, row) {
      console.log(row);
      this.deleteRecommend(row.sourceId, row.sourceType);
    },
    deleteRecommend(sourceId, sourceType) {
      this.$confirm("是否要删除该内容的首页推荐?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        const params = {
          sourceId: sourceId,
          sourceType: sourceType,
        };
        // params.append("id", ids);
        recommendDelById(params).then((response) => {
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
    getList() {
      // console.log(this.listQuery);
      this.listLoading = true;
      recommendList(this.listQuery).then((response) => {
        this.listLoading = false;
        console.log(response.data);
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
  },
  filters: {
    formatCreateTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      const date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    },
  },
};
</script>

<style scoped>
</style>
